2 require_once('include/nusoap/nusoap.php');
3 require_once('include/TimeDate.php');
5 * This class is meant to test everything SOAP
8 class SOAPAPI2Test extends Sugar_PHPUnit_Framework_TestCase
11 public $_soapClient = null;
12 public $_session = null;
13 public $_sessionId = '';
14 public $_contactId = '';
20 public function setUp()
22 $this->_soapClient = new nusoapclient($GLOBALS['sugar_config']['site_url'].'/service/v2/soap.php',false,false,false,false,false,600,600);
23 $this->_setupTestUser();
27 * Remove anything that was used during this test
30 public function tearDown() {
31 global $soap_version_test_accountId, $soap_version_test_opportunityId, $soap_version_test_contactId;
32 $this->_tearDownTestUser();
34 $this->_sessionId = '';
35 unset($soap_version_test_accountId);
36 unset($soap_version_test_opportunityId);
37 unset($soap_version_test_contactId);
41 * Ensure we can create a session on the server.
44 public function testCanLogin(){
45 $result = $this->_login();
46 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
47 'SOAP Session not created. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
50 public function testSetEntryForContact() {
51 global $soap_version_test_contactId;
52 $result = $this->_setEntryForContact();
53 $soap_version_test_contactId = $result['id'];
54 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
55 'Can not create new contact. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
58 public function testGetEntryForContact() {
59 $result = $this->_getEntryForContact();
60 if (empty($this->_soapClient->faultcode)) {
61 if (($result['entry_list'][0]['name_value_list'][2]['value'] == 1) &&
62 ($result['entry_list'][0]['name_value_list'][3]['value'] == "Cold Call") &&
63 ($result['relationship_list'][0][0]['records'][0][1]['value'] == 'contact@sugar.com')) {
65 $this->assertEquals($result['entry_list'][0]['name_value_list'][2]['value'],1,"testGetEntryForContact method - Get Entry For contact is not same as Set Entry");
68 $this->assertTrue(empty($this->_soapClient->faultcode), 'Can not retrieve newly created contact. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
75 public function testGetEntryForContactNoSelectFields(){
76 global $soap_version_test_contactId;
78 $result = $this->_soapClient->call('get_entry',array('session'=>$this->_sessionId,'module_name'=>'Contacts','id'=>$soap_version_test_contactId,'select_fields'=>array(), 'link_name_to_fields_array' => array()));
79 $this->assertTrue(!empty($result['entry_list'][0]['name_value_list']), "testGetEntryForContactNoSelectFields returned no field data");
83 public function testSetEntriesForAccount() {
84 $result = $this->_setEntriesForAccount();
85 $this->assertTrue(!empty($result['ids']) && $result['ids'][0] != -1,
86 'Can not create new account using testSetEntriesForAccount. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
89 public function testSetEntryForOpportunity() {
90 $result = $this->_setEntryForOpportunity();
91 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
92 'Can not create new account using testSetEntryForOpportunity. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
95 public function testSetRelationshipForOpportunity() {
96 $result = $this->_setRelationshipForOpportunity();
97 $this->assertTrue(($result['created'] > 0), 'testSetRelationshipForOpportunity method - Relationship for opportunity to Contact could not be created');
102 public function testGetRelationshipForOpportunity()
104 global $soap_version_test_contactId;
105 $result = $this->_getRelationshipForOpportunity();
107 $result['entry_list'][0]['id'],
108 $soap_version_test_contactId,
109 "testGetRelationshipForOpportunity - Get Relationship of Opportunity to Contact failed"
113 public function testSearchByModule() {
114 $result = $this->_searchByModule();
115 $this->assertTrue(($result['entry_list'][0]['records'] > 0 && $result['entry_list'][1]['records'] && $result['entry_list'][2]['records']), "testSearchByModule - could not retrieve any data by search");
118 /**********************************
119 * HELPER PUBLIC FUNCTIONS
120 **********************************/
123 * Attempt to login to the soap server
125 * @return $set_entry_result - this should contain an id and error. The id corresponds
128 public function _login(){
129 global $current_user;
130 $result = $this->_soapClient->call('login',
132 array('user_name' => $current_user->user_name,
133 'password' => $current_user->user_hash,
135 'application_name' => 'SoapTest',
136 'name_value_list'=>array())
138 $this->_sessionId = $result['id'];
142 public function _setEntryForContact() {
145 $current_date = $timedate->convert_to_gmt_datetime('now');
147 $first_name = 'SugarContactFirst' . $time;
148 $last_name = 'SugarContactLast';
149 $email1 = 'contact@sugar.com';
150 $result = $this->_soapClient->call('set_entry',array('session'=>$this->_sessionId,'module_name'=>'Contacts', 'name_value_list'=>array(array('name'=>'last_name' , 'value'=>"$last_name"), array('name'=>'first_name' , 'value'=>"$first_name"), array('name'=>'do_not_call' , 'value'=>"1"), array('name'=>'birthdate' , 'value'=>"$current_date"), array('name'=>'lead_source' , 'value'=>"Cold Call"), array('name'=>'email1' , 'value'=>"$email1"))));
151 SugarTestContactUtilities::setCreatedContact(array($this->_contactId));
155 public function _getEntryForContact() {
156 global $soap_version_test_contactId;
158 $result = $this->_soapClient->call('get_entry',array('session'=>$this->_sessionId,'module_name'=>'Contacts','id'=>$soap_version_test_contactId,'select_fields'=>array('last_name', 'first_name', 'do_not_call', 'lead_source', 'email1'), 'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address'))))); $GLOBALS['log']->fatal("_getEntryForContact" . " " . $soap_version_test_contactId);
162 public function _setEntriesForAccount() {
163 global $soap_version_test_accountId;
166 $current_date = $timedate->convert_to_gmt_datetime('now');
168 $name = 'SugarAccount' . $time;
169 $email1 = 'account@'. $time. 'sugar.com';
170 $result = $this->_soapClient->call('set_entries',array('session'=>$this->_sessionId,'module_name'=>'Accounts', 'name_value_lists'=>array(array(array('name'=>'name' , 'value'=>"$name"), array('name'=>'email1' , 'value'=>"$email1")))));
171 $soap_version_test_accountId = $result['ids'][0];
172 $GLOBALS['log']->fatal("_setEntriesForAccount id = " . $soap_version_test_accountId);
173 SugarTestAccountUtilities::setCreatedAccount(array($soap_version_test_accountId));
177 public function _setEntryForOpportunity() {
178 global $soap_version_test_accountId, $soap_version_test_opportunityId;
181 $date_closed = $timedate->convert_to_gmt_datetime(strtotime('+1 week'));
183 $name = 'SugarOpportunity' . $time;
184 $account_id = $soap_version_test_accountId;
185 $sales_stage = 'Prospecting';
188 $GLOBALS['log']->fatal("_setEntryForOpportunity id = " . $soap_version_test_accountId);
189 $result = $this->_soapClient->call('set_entry',array('session'=>$this->_sessionId,'module_name'=>'Opportunities', 'name_value_lists'=>array(array('name'=>'name' , 'value'=>"$name"), array('name'=>'amount' , 'value'=>"$amount"), array('name'=>'probability' , 'value'=>"$probability"), array('name'=>'sales_stage' , 'value'=>"$sales_stage"), array('name'=>'account_id' , 'value'=>"$account_id"))));
190 $soap_version_test_opportunityId = $result['id'];
194 public function _getEntryForOpportunity() {
195 global $soap_version_test_opportunityId;
197 $result = $this->_soapClient->call('get_entry',array('session'=>$this->_sessionId,'module_name'=>'Opportunities','id'=>$soap_version_test_opportunityId,'select_fields'=>array('name', 'amount'), 'link_name_to_fields_array' => array(array('name' => 'contacts', 'value' => array('id', 'first_name', 'last_name'))))); $GLOBALS['log']->fatal("_getEntryForContact" . " " . $soap_version_test_opportunityId);
201 public function _setRelationshipForOpportunity() {
203 global $soap_version_test_contactId, $soap_version_test_opportunityId;
205 $result = $this->_soapClient->call('set_relationship',array('session'=>$this->_sessionId,'module_name' => 'Opportunities','module_id' => "$soap_version_test_opportunityId", 'link_field_name' => 'contacts','related_ids' =>array("$soap_version_test_contactId"), 'name_value_list' => array(array('name' => 'contact_role', 'value' => 'testrole')), 'delete'=>0));
209 public function _getRelationshipForOpportunity() {
210 global $soap_version_test_opportunityId;
212 $result = $this->_soapClient->call('get_relationships',
214 'session' => $this->_sessionId,
215 'module_name' => 'Opportunities',
216 'module_id' => "$soap_version_test_opportunityId",
217 'link_field_name' => 'contacts',
218 'related_module_query' => '',
219 'related_fields' => array('id'),
220 'related_module_link_name_to_fields_array' => array(array('name' => 'contacts', 'value' => array('id', 'first_name', 'last_name'))),
227 public function _searchByModule() {
229 $result = $this->_soapClient->call('search_by_module',
231 'session' => $this->_sessionId,
232 'search_string' => 'Sugar',
233 'modules' => array('Accounts', 'Contacts', 'Opportunities'),
235 'max_results' => '10')
244 public function _setupTestUser() {
245 $this->_user = SugarTestUserUtilities::createAnonymousUser();
246 $this->_user->status = 'Active';
247 $this->_user->is_admin = 1;
248 $this->_user->save();
249 $GLOBALS['current_user'] = $this->_user;
253 * Remove user created for test
256 public function _tearDownTestUser() {
257 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
258 unset($GLOBALS['current_user']);